/*
 * Copyright (C) 2018 Logos Technology Ltd.
 */

package com.logos.tool;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Created by hk on 2018/4/19.
 */
public class TimeUtil {

    private static final long ONE_SECOND = 1000;
    private static final long ONE_MINUTE = ONE_SECOND * 60;
    private static final long ONE_HOUR = ONE_MINUTE * 60;
    private static final long ONE_DAY = ONE_HOUR * 24;

    /**
     * 获取今年第一天的时间戳
     *
     * @return long
     */
    public static long getYearFirstDay() {
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        calendar.set(Calendar.YEAR, currentYear);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        //将小时至0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        calendar.set(Calendar.MINUTE, 0);
        //将秒至0
        calendar.set(Calendar.SECOND, 0);
        //将毫秒至0
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis() / 1000;
    }

    /**
     * 获取今年最后一天的时间戳
     *
     * @return long
     */
    public static long getYearEndDay() {
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        calendar.set(Calendar.YEAR, currentYear + 1);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        //将小时至0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        calendar.set(Calendar.MINUTE, 0);
        //将秒至0
        calendar.set(Calendar.SECOND, 0);
        //将毫秒至0
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis() / 1000;
    }

    /**
     * getMonthFirstDay
     *
     * @return long
     */
    public static long getMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        //将小时至0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        calendar.set(Calendar.MINUTE, 0);
        //将秒至0
        calendar.set(Calendar.SECOND, 0);
        //将毫秒至0
        calendar.set(Calendar.MILLISECOND, 0);
        // 获取本月第一天的时间戳
        return calendar.getTimeInMillis() / 1000;
    }

    /**
     * getMonthLastDay
     *
     * @return long
     */
    public static long getMonthLastDay() {
        //获取当前月最后一天
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        //将小时至0
        ca.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        ca.set(Calendar.MINUTE, 0);
        //将秒至0
        ca.set(Calendar.SECOND, 0);
        //将毫秒至0
        ca.set(Calendar.MILLISECOND, 0);
        // 获取本月最后一天的时间戳
        return ca.getTimeInMillis() / 1000;
    }

    /**
     * getTodayStart
     *
     * @return long
     */
    public static long getTodayStart() {
        long current = System.currentTimeMillis();//当前时间毫秒数
        long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24)
                - TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
        return zero / 1000;
    }

    /**
     * getTodayEnd
     *
     * @return long
     */
    public static long getTodayEnd() {
        long current = System.currentTimeMillis();//当前时间毫秒数
        long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24)
                - TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
        long twelve = zero + 24 * 60 * 60 * 1000 - 1;//今天23点59分59秒的毫秒数
        return twelve / 1000;
    }

    /**
     * Java将Unix时间戳转换成指定格式日期字符串
     *
     * @param timestamp 时间戳 如："1473048265";
     * @return 返回结果 如："2016-09-05 16:06:42";
     */
    public static String timeStamp2Date(long timestamp) {
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
                .format(new Date(timestamp));
        return date;
    }

    /**
     * Java将Unix时间戳转换成指定格式日期字符串
     *
     * @param timestamp 时间戳 如："1473048265";
     * @param formats   要格式化的格式 默认："yyyy-MM-dd HH:mm:ss";
     * @return 返回结果 如："2016-09-05 16:06:42";
     */
    public static String timeStamp2Date(long timestamp, String formats) {
        if (TextUtils.isEmpty(formats)) {
            formats = "yyyy-MM-dd HH:mm:ss";
        }
        String date = new SimpleDateFormat(formats, Locale.CHINA)
                .format(new Date(timestamp));
        return date;
    }

    /**
     * Java将Unix时间戳转换成指定格式日期字符串
     *
     * @param timestamp 时间戳 如："1473048265";
     * @return 返回结果 如："2016-09-05 16:06:42";
     */
    public static String timeStamp2Date(int timestamp) {
        long timestampLong = (long) timestamp * 1000;
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
                .format(new Date(timestampLong));
        return date;
    }

    /**
     * Java将Unix时间戳转换成指定格式日期字符串
     *
     * @param timestamp 时间戳 如："1473048265";
     * @param formats   要格式化的格式 默认："yyyy-MM-dd HH:mm:ss";
     * @return 返回结果 如："2016-09-05 16:06:42";
     */
    public static String timeStamp2Date(int timestamp, String formats) {
        if (TextUtils.isEmpty(formats)) {
            formats = "yyyy-MM-dd HH:mm:ss";
        }
        long timestampLong = (long) timestamp * 1000;
        String date = new SimpleDateFormat(formats, Locale.CHINA)
                .format(new Date(timestampLong));
        return date;
    }

    /**
     * Java将Unix时间戳转换成指定格式日期字符串
     *
     * @param timestampString 时间戳 如："1473048265";
     * @return 返回结果 如："2016-09-05 16:06:42";
     */
    public static String timeStamp2Date(String timestampString) {
        Long timestamp = Long.parseLong(timestampString) * 1000;
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
                .format(new Date(timestamp));
        return date;
    }

    /**
     * Java将Unix时间戳转换成指定格式日期字符串
     *
     * @param timestampString 时间戳 如："1473048265";
     * @param formats         要格式化的格式 默认："yyyy-MM-dd HH:mm:ss";
     * @return 返回结果 如："2016-09-05 16:06:42";
     */
    public static String timeStamp2Date(String timestampString, String formats) {
        if (TextUtils.isEmpty(formats)) {
            formats = "yyyy-MM-dd HH:mm:ss";
        }
        Long timestamp = Long.parseLong(timestampString) * 1000;
        String date = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
        return date;
    }

    /**
     * 将国际时间转换成一定格式的北京时间
     *
     * @param utcStr  字符串日期
     * @param pattern 时间格式比如yyyy-MM-dd
     * @return 毫秒值
     */
    public static long timeStringToLong(String utcStr, String pattern) {
        //格式化时间
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = sdf.parse(utcStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));
        return calendar.getTimeInMillis();
    }

    /**
     * 时间转换，如果是 "yyyy-MM-dd'T'HH:mm:ss'+'00008" 就不加8，否则要加8
     *
     * @param utcStr utcStr
     * @param format format
     * @return string
     */
    public static String utcToCst(String utcStr, String format) {
        //格式化时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'+'SSSS");
        //解析时间 2016-01-05T15:06:58.000Z
        // 2017-06-11T14:00:00+0800
        Date date = null;
        try {
            date = sdf.parse(utcStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY));

        return dateFormat.format(calendar.getTime());
    }


    /**
     * 将国际时间转成时间戳
     *
     * @param UTCString UTCString
     * @return long
     */
    public static long utcStringToDefaultLong(String UTCString) {
        try {
            if (!TextUtils.isEmpty(UTCString)) {
                UTCString = UTCString.replace("Z", " UTC");
                SimpleDateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
                Date date = utcFormat.parse(UTCString);
                return date.getTime();
            } else {
                return 0;
            }

        } catch (ParseException pe) {
            pe.printStackTrace();
            return 0;
        }
    }

    /**
     * 将国际时间转换成你想要使用的时间格式
     *
     * @param UTCString UTCString
     * @param format    时间格式
     * @return string
     */
    public static String utcStringToDefaultString(String UTCString, String format) {
        try {
            if (!TextUtils.isEmpty(UTCString)) {
                UTCString = UTCString.replace("Z", " UTC");
                SimpleDateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
                Date date = utcFormat.parse(UTCString);
                SimpleDateFormat dateFormat = new SimpleDateFormat(format);
                return dateFormat.format(date);
            } else {
                return null;
            }

        } catch (ParseException pe) {
            pe.printStackTrace();
            return null;
        }
    }

    /**
     * 根据国际时间，获取当前是周几
     *
     * @param UTCString UTCString
     * @return string
     */
    public static String utcStringToWeek(String UTCString) {
        if (!TextUtils.isEmpty(UTCString)) {
            String week = "周";
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(utcStringToDefaultLong(UTCString));
            int i = calendar.get(Calendar.DAY_OF_WEEK);
            switch (i) {
                case 1:
                    week += "日";
                    break;
                case 2:
                    week += "一";
                    break;
                case 3:
                    week += "二";
                    break;
                case 4:
                    week += "三";
                    break;
                case 5:
                    week += "四";
                    break;
                case 6:
                    week += "五";
                    break;
                case 7:
                    week += "六";
                    break;
            }
            return week;
        } else {
            return "不存在";
        }
    }


    /**
     * 获取目标时间和当前时间之间的差距.
     *
     * @return 获取目标时间和当前时间之间的差距
     */
    public static String getTimestampString(long timeStamp) {
        long longTimeStamp = new Long(new Long(timeStamp) * 1000);
        Date date = new Date(longTimeStamp);
        Date curDate = new Date();
        long splitTime = curDate.getTime() - date.getTime();
        if (splitTime < ( ONE_DAY)) {
            return "今天";
        }else if (splitTime<2*ONE_DAY){
            return "昨天";
        }else if (splitTime<3*ONE_DAY){
            return "前天";
        }
        String result;
        result = "yyyy.MM.dd";
        return (new SimpleDateFormat(result, Locale.CHINA)).format(date);
    }
}
